#include "head.h"

int cdFunc(const char* arg,stack1_t* dir){
    char path[4096];
    onePath(arg,path,dir);
    printf("path= %s\n",path);
    if(access(path,F_OK)==-1){//看看是否存在这个文件
        printf("access is error\n");
        return -1;
    }
    char argv[4096];
    strcpy(argv,arg);//复制
    if(arg[0]=='/'){//这是根目录
        for(int i=0;i<dir->stacksize;i++){
            char str[256];
            pop(dir,str);
        }
        travel_stack(*dir,argv);
        char* p;
        strtok(path,"/");
        strtok(NULL,"/");
        while((p=strtok(NULL,"/"))!=NULL){
            push(dir,p);
        }
    }
    else if(arg[0]=='.'  && arg[1]=='.'){
        if(dir->stacksize==1){
            return 0;
        }
        printf("now is ..\n");
        printf("stacksize : %d\n",dir->stacksize);
        char str[256]={0};
        pop(dir,str);
        char* p;
        if((p=strtok(argv,"/"))==NULL){
            return 0;
        }

        while((p=strtok(NULL,"/"))!=NULL){
            push(dir,p);
        }
    }
    else{
        if(arg[0]=='.' && arg[1]=='\0' || arg[0]=='\0'){
            return 0;
        }
        char* p;
        p=strtok(argv,"/");
        if(p==NULL){
            return 0;
        }
        if(p[0]!='.'){
            push(dir,p);
        }
        while((p=strtok(NULL,"/"))!=NULL){
            push(dir,p);
        }
    }
    return 0;
}
